package com.example.projectmobile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {
	private PlaceDataSource datasrc;
	private ListView listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listview = (ListView) findViewById(android.R.id.list);
		datasrc=new PlaceDataSource(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void onClick(View view) { //click add button 
		Intent intent = new Intent(MainActivity.this, AddActivity.class);
		startActivity(intent); //start AddActivity
	}
	
	@Override
	protected void onResume() { 
		// TODO Auto-generated method stub
		super.onResume();
		datasrc.open();
		ArrayList<Place> records = datasrc.getAllRecord();

		final ArrayList<String> list = new ArrayList<String>();//Add item to list
		for (int i = 0; i < records.size(); ++i) {

			list.add(records.get(i).getId()+" "+records.get(i).getName());
		}

		final StableArrayAdapter adapter = new StableArrayAdapter(this,
				android.R.layout.simple_list_item_1, list);

		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {   //on item click send id to detailActivity 
				// TODO Auto-generated method stub
				final String item = (String) parent.getItemAtPosition(position);
				String myId[]= item.split(" ");
				
				Intent intent = new Intent(MainActivity.this, DetailActivity.class);

				intent.putExtra("ID", (Integer.parseInt(myId[0])));

				startActivity(intent);//start DetailActivity
			}
			
		});
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		datasrc.close();
	}
	
	private class StableArrayAdapter extends ArrayAdapter<String> {
		HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

		public StableArrayAdapter(Context context, int textViewResourceId,
				List<String> objects) {
			super(context, textViewResourceId, objects);
			for (int i = 0; i < objects.size(); ++i) {
				mIdMap.put(objects.get(i), i);
			}
		}

		@Override
		public long getItemId(int position) {
			String item = getItem(position);
			return mIdMap.get(item);
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}
	}

}
